<link rel="stylesheet" href="nepadmin/lay/extends/dtree/dtree.css">
<link rel="stylesheet" href="nepadmin/lay/extends/dtree/font/iconfont.css">
<div class="layui-fluid" id="VIEW-list-table" lay-title="文章管理">
    <div class="layui-row layui-col-space12">
        <div class="layui-col-md3">
            <div class="layui-card">
                <div class="layui-card-body nepadmin-table-full">
                    <ul id="demoTree1" class="dtree" data-id="0"></ul>
                </div>
            </div>
        </div>

        <div class="layui-col-md9">
            <div class="layui-card">
                <div class="layui-card-body nepadmin-table-full">
                    <div class="layui-btn-container nepadmin-pad-t10 nepadmin-pad-l10">
                        <div class="layui-btn layui-btn-sm add">添加
                        </div>
                        <div class="layui-btn layui-btn-sm layui-btn-primary table-action" data-type="del">删除</div>
                    </div>
                    <table id="list-table-cmsArticle" lay-filter="list-table-cmsArticle"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    layui.use(['admin', 'table', 'form', 'conf', 'jquery', 'dtree'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , admin = layui.admin
            , conf = layui.conf
            , dtree = layui.dtree
            , table = layui.table;

        var view = $('#VIEW-list-table');
        var tableFilter = 'list-table-cmsArticle';
        var treeUrl = conf.requestUrl + '/cmsCategory/queryCategoryListR?token=' + admin.loginToken;
        // 定义树
        dtree.render({
            elem: "#demoTree1",  //绑定元素
            method: "get",
            dataStyle: "layuiStyle",
            response: {statusCode: 0, rootName: "cmsCategoryList",title:"name",basicData:"code"},
            initLevel: 1,
            dot: false,
            skin: "zdy",  // 自定义风格
            url: treeUrl  //异步接口
        });

        //单击节点 监听事件
        dtree.on("node('demoTree1')" ,function(param){
            // layer.msg(param.basicData);
            var categoryCode =param.basicData.slice(1, param.basicData.length-1);
            // 搜索
            table.reload('list-table-cmsArticle',{
                url:'/cmsArticle/page?token='+admin.loginToken,
                page:{
                    curr:1
                },
                where: {
                    categoryCode:categoryCode
                }
            })
        });

        var url = '/cmsArticle/page?token=' + admin.loginToken;
        table.render({
            elem: '[lay-filter="' + tableFilter + '"]',
            //api: 'getGoods',
            url: url,
            height: 'full-165',
            cols: [[
                {title: '', type: 'checkbox', fixed: true, width: 60},
                {title: '标题', field: 'title'},
                {title: '作者', field: 'author'},
                {title: '关键字', field: 'keywords'},
                {
                    title: '类型', width: 80, templet: function (d) {
                    var status = {
                        1: {title: '普通', color: 'blue'},
                        2: {title: '热门', color: 'orange'},
                    }[d.type];
                    return '<span class="layui-badge layui-bg-' + status.color + '">' + status.title + '</span>';
                }
                },
                {
                    title: '操作时间', field: 'time', align: 'center', templet: function (d) {
                    var createTime = layui.util.toDateString(d.createTime, 'yyyy-MM-dd')
                    return '<p><span title="' + d.createTime + '" class="nepadmin-c-gray">' + createTime + '</span></p>';
                }
                },
                {
                    title: '操作', width: 150, align: 'center', templet: function (d) {
                    var editBtn = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                    var delBtn = '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                    return editBtn + delBtn;
                }
                }
            ]]
        });

        table.on('tool(' + tableFilter + ')', function (obj) {
            if (obj.event === 'del') {//删除行

            } else if (obj.event === "edit") {
                admin.popup({
                    url: '/cms/article/cmsArticleEdit',
                    area: ['60%', '80%'],
                    maxmin: true,
                    data : {
                        id : obj.data.id,
                        table:table
                    }
                })
                return false
            }
        });

        view.find('.add').click(function () {
            admin.popup({
                url: '/cms/article/cmsArticleEdit',
                area: ['60%', '80%'],
                maxmin: true,
                data : {
                    id:'',
                    table:table
                }
            })
        });

        view.find('.table-action').click(function () {
            var checkedCount = table.checkStatus(tableFilter).data.length;
            if (checkedCount == 0) {
                layer.msg('请先选择某行');
            } else {
                layer.msg($(this).html() + checkedCount + '项');
            }
        })
    })
</script>